﻿Public Class CCAPTimeSetForm

    '起始地址
    Private _offset As UShort = 0

    '时间参数数据
    Private _buf() As Short

    Private Sub TimeSetForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        EnableModify(False)
        Me.btnModify.Enabled = False
        Timer1.Start() ' LoadData()
    End Sub


    Private Sub LoadData()
        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = COMHelper.Instance.Send4Short(g_netID, FunctionCode.WordReadonly, _offset, 7) '读1号子控时间
            If _buf Is Nothing Then
                msg.ShowError(g_strReadError)
                Me.nudYear.Minimum = 0
                Me.nudYear.Value = 0
                Me.btnModify.Enabled = False
                Me.btnShowDevTime.Enabled = False
                Exit Sub
            End If
        End If

        Me.btnModify.Enabled = True

        Try
            Me.nudYear.Value = _buf(0)
            Me.nudMonth.Value = _buf(1)
            Me.nudDay.Value = _buf(2)
            Me.nudHour.Value = _buf(3)
            Me.nudMinute.Value = _buf(4)
            Me.nudSecond.Value = _buf(5)
        Catch ex As Exception
            msg.ShowError(ex.Message)
        End Try


    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

 

    Private Sub EnableModify(ByVal value As Boolean)
        Me.btnModify.Enabled = Not value
        Me.btnOk.Enabled = value
        Me.btnShowDevTime.Enabled = Not value

        Me.nudYear.ReadOnly = Not value
        Me.nudMonth.ReadOnly = Not value
        Me.nudDay.ReadOnly = Not value
        Me.nudHour.ReadOnly = Not value
        Me.nudMinute.ReadOnly = Not value
        Me.nudSecond.ReadOnly = Not value

        Dim i As Integer
        If value Then
            i = 1
        Else
            i = 0
        End If

        Me.nudYear.Increment = i
        Me.nudMonth.Increment = i
        Me.nudDay.Increment = i
        Me.nudHour.Increment = i
        Me.nudMinute.Increment = i
        Me.nudSecond.Increment = i

    End Sub

    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click
        EnableModify(True)
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim result As Boolean
        Me.Cursor = Cursors.WaitCursor
        result = save()
        Me.Cursor = Cursors.Default

        If result Then
            msg.Show("保存成功")
        Else
            Exit Sub
        End If

        EnableModify(False)
    End Sub

    ''' <summary>
    ''' 保存数据
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean
        Dim value As Short

        Dim result As Boolean = False

        value = Me.nudYear.Value '- 1900
        If Not Write(0, value) Then
            GoTo WriteERROR
        End If
        sleep()

        value = Me.nudMonth.Value
        If Not Write(1, value) Then
            GoTo WriteERROR
        End If
        sleep()

        value = Me.nudDay.Value
        If Not Write(2, value) Then
            GoTo WriteERROR
        End If
        sleep()

        value = Me.nudHour.Value
        If Not Write(3, value) Then
            GoTo WriteERROR
        End If
        sleep()

        value = Me.nudMinute.Value
        If Not Write(4, value) Then
            GoTo WriteERROR
        End If
        sleep()

        value = Me.nudSecond.Value
        If Not Write(5, value) Then
            GoTo WriteERROR
        End If
        sleep()

        'value = Me.nudWeek.Value
        'COMHelper.Instance.Broadcast(functionCode, 6, value)
        'sleep()

        '修改使能位
        value = 1
        If Not Write(7, value) Then
            GoTo WriteERROR
        End If

        GoTo WriteSUCCESS

WriteERROR:
        msg.ShowError("修改时间失败")
        Return False

WriteSUCCESS:
        Return True

    End Function

    Private Function ReadAllDevTime() As String

        ReadAllDevTime = String.Empty
        Dim str As String = Nothing

        str = _ReadDevTime(255)
        If str IsNot Nothing Then
            ReadAllDevTime &= str & vbCrLf
        Else
            ReadAllDevTime = String.Empty
            msg.ShowError(g_strReadError)
            Exit Function
        End If
        For Each item As CcapDevItem In MainForm.pi.CcappDevList

            Try
                str = _ReadDevTime(item.NetId)
                If str IsNot Nothing Then
                    ReadAllDevTime &= str & vbCrLf
                Else
                    ReadAllDevTime &= item.NetId & "号设备系统时间：" & g_strReadError & vbCrLf
                    'ReadAllDevTime = String.Empty
                    'msg.ShowError(g_strReadError)
                    'Exit Function
                End If
            Catch ex As Exception
                ReadAllDevTime = String.Empty
                msg.ShowError(ex.Message)
                Exit Function
            End Try
        Next

    End Function
    Private Function _ReadDevTime(ByVal netID As Integer) As String
        _ReadDevTime = String.Empty

        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = __ReadDevTime(netID)
            If _buf Is Nothing Then
                Me.nudYear.Minimum = 0
                Me.nudYear.Value = 0
                'Me.btnModify.Enabled = False
                Return Nothing
            End If
        End If

        If netID = 255 Then
            _ReadDevTime &= " 主设备系统时间："
        Else
            _ReadDevTime &= netID & "号设备系统时间："
        End If

        If netID = g_netID Then
            Me.nudYear.Value = _buf(0)
            Me.nudMonth.Value = _buf(1)
            Me.nudDay.Value = _buf(2)
            Me.nudHour.Value = _buf(3)
            Me.nudMinute.Value = _buf(4)
            Me.nudSecond.Value = _buf(5)
        End If

        _ReadDevTime &= String.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", _
            _buf(0), _buf(1), _buf(2), _buf(3), _buf(4), _buf(5))

    End Function
    Private Function __ReadDevTime(ByVal netID As Integer) As Short()

        If g_test Then
            ReDim _buf(200)
            For i As Integer = 0 To _buf.Length - 1
                _buf(i) = g_rand.Next(1, 12)
            Next
        Else
            _buf = COMHelper.Instance.Send4Short(netID, FunctionCode.WordReadonly, _offset, 7) '读1号子控时间
        End If

        Return _buf
    End Function

    Private Function Write(ByVal offset As UShort, ByVal value As Short) As Boolean        
        Return COMHelper.Instance.Write(g_broadID, FunctionCode.WordWrite, offset, value) '广播地址
    End Function

    Sub Sleep()
        System.Threading.Thread.Sleep(100)
    End Sub

    Private Sub nudYear_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudYear.ValueChanged

    End Sub

    Private Sub nudMonth_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudMonth.ValueChanged

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        Me.btnModify.Enabled = True
        LoadData()
    End Sub

    Private Sub btnShowDevTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowDevTime.Click
        sender.Enabled = False
        Me.btnModify.Enabled = False
        Dim str As String
        str = ReadAllDevTime()
        If str.Length > 0 Then
            msg.Show(str)
        End If
        sender.Enabled = True
        Me.btnModify.Enabled = True
    End Sub
End Class